* Cost of voting Peru
* Gianmarco Leon

* Run direct loc.
do	"/Users/Gianmarco 1/Dropbox/Research Projects/Cost of voting (Peru)/Do/Direc_loc_GL.do"
*do	"/Users/Gianmarco/Dropbox/Research Projects/Cost of voting (Peru)/Do/Direc_loc_GL_2.do"

**********
*** Tables
**********
clear all
clear matrix
set more off
cap log close

use		"$data/Voting_Panel_Replication.dta", clear

cd "$out"

* I EXCLUDE FROM HERE ALL OBSERVATIONS THAT DO NOT ENTER THE ANALYSIS (But keep the extreme poor here)
global controls="gender age educ lgastopc pobre3 pobre2"
global instruments="nopobretreat pobretreat"
xi: ivreg2		vote102 (deltam=$instruments) $controls i.ubigeo_ccpp if panel_complete==1, cl(hhid)    partial(i.ubigeo_ccpp) nocons
keep if e(sample)

* Table 1: Turnout and Perceived Fine

* Panel A: Perceived Fines

file open fh using "Perceived_fines.tex", write replace
	file write fh "\begin{tabular}{l c c c c c l}\hline\hline" _n
	file write fh " & \textbf{Obs.} & \textbf{Total} & \textbf{Treatment} & \textbf{Control} & \textbf{T - C} &  \textbf{P-value} \\" _n

file write fh "\multicolumn{1}{l}{\textbf{Baseline}}& & & & & & \\" _n
file write fh "\hline" _n

	local name1="Extreme Poor" 
	local name2="Poor" 
	local name3="Non-Poor" 
	
foreach num of numlist 3 2 1 {
	qui sum multa_1 if pobreza==`num'
			local 	mean0=string(r(mean),"%5.2f")
	qui sum multa_1 if pobreza==`num' & treat==1
			local 	mean1=string(r(mean),"%5.2f")	
	qui sum multa_1 if pobreza==`num' & treat==0
			local 	mean2=string(r(mean),"%5.2f")	
	qui ttest multa_1 if pobreza==`num', by(treat)
			local diff1=string(r(mu_2)-r(mu_1),"%5.2f")
			local pdiff1=string((r(p)),"%5.2f")
			local N1=string(r(N_1)+r(N_2),"%-9.0f")
	
file write fh "\multicolumn{1}{l}{`name`num''}& `N1'& `mean0' & `mean1' & `mean2' & `diff1' & (`pdiff1') \\" _n
}
file write fh "\hline" _n
	qui sum multa_1 
			local 	mean0=string(r(mean),"%5.2f")
	qui sum multa_1 if treat==1
			local 	mean1=string(r(mean),"%5.2f")	
	qui sum multa_1 if treat==0
			local 	mean2=string(r(mean),"%5.2f")	
	qui ttest multa_1, by(treat)
		local diff1=string(r(mu_2)-r(mu_1),"%5.2f")
		local pdiff1=string((r(p)),"%5.2f")
		local N1=string(r(N_1)+r(N_2),"%-9.0f")
		
file write fh "\multicolumn{1}{l}{Total}& `N1'& `mean0' & `mean1' & `mean2' & `diff1' & (`pdiff1') \\" _n
file write fh "\hline" _n

file write fh "\multicolumn{1}{l}{\textbf{Follow-up}}& & & & & & \\" _n
file write fh "\hline" _n
foreach num of numlist 3 2 1 {
	qui sum multa_2 if pobreza==`num'
		local 	mean0=string(r(mean),"%5.2f")
	qui sum multa_2 if pobreza==`num' & treat==1
		local 	mean1=string(r(mean),"%5.2f")	
	qui sum multa_2 if pobreza==`num' & treat==0
		local 	mean2=string(r(mean),"%5.2f")	
	qui ttest multa_2 if pobreza==`num', by(treat)
		local diff1=string(r(mu_2)-r(mu_1),"%5.2f")
		local pdiff1=string((r(p)),"%5.2f")
		local N1=string(r(N_1)+r(N_2),"%-9.0f")

file write fh "\multicolumn{1}{l}{`name`num''}& `N1'& `mean0' & `mean1' & `mean2' & `diff1' & (`pdiff1') \\" _n
}
file write fh "\hline" _n
	qui sum multa_2 
		local 	mean0=string(r(mean),"%5.2f")
	qui sum multa_2 if treat==1
		local 	mean1=string(r(mean),"%5.2f")	
	qui sum multa_2 if treat==0
		local 	mean2=string(r(mean),"%5.2f")	
	qui ttest multa_2, by(treat)
		local diff1=string(r(mu_2)-r(mu_1),"%5.2f")
		local pdiff1=string((r(p)),"%5.2f")
		local N1=string(r(N_1)+r(N_2),"%-9.0f")

file write fh "\multicolumn{1}{l}{Total}& `N1'& `mean0' & `mean1' & `mean2' & `diff1' & (`pdiff1') \\" _n
file write fh "\hline" _n

file write fh "\multicolumn{1}{l}{\textbf{Change}}& & & & & & \\" _n
file write fh "\hline" _n
foreach num of numlist 3 2 1 {
	qui sum deltam if pobreza==`num'
			local 	mean0=string(r(mean),"%5.2f")
	qui sum deltam if pobreza==`num' & treat==1
			local 	mean1=string(r(mean),"%5.2f")	
	qui sum deltam if pobreza==`num' & treat==0
			local 	mean2=string(r(mean),"%5.2f")	
	qui ttest deltam if pobreza==`num', by(treat)
			local diff1=string(r(mu_2)-r(mu_1),"%5.2f")
			local pdiff1=string((r(p)),"%5.2f")
			local N1=string(r(N_1)+r(N_2),"%-9.0f")

file write fh "\multicolumn{1}{l}{`name`num''}& `N1'& `mean0' & `mean1' & `mean2' & `diff1' & (`pdiff1') \\" _n
}
file write fh "\hline" _n
	qui sum deltam
			local 	mean0=string(r(mean),"%5.2f")
	qui sum deltam if treat==1
			local 	mean1=string(r(mean),"%5.2f")	
	qui sum deltam if treat==0
			local 	mean2=string(r(mean),"%5.2f")	
	qui ttest deltam, by(treat)
			local diff1=string(r(mu_2)-r(mu_1),"%5.2f")
			local pdiff1=string((r(p)),"%5.2f")
			local N1=string(r(N_1)+r(N_2),"%-9.0f")
			
file write fh "\multicolumn{1}{l}{Total}& `N1'& `mean0' & `mean1' & `mean2' & `diff1' & (`pdiff1') \\" _n
		
file write fh "\hline" _n
file write fh "\hline" _n
file write fh "\end{tabular}" _n
file close fh

* Panel B: Turnout
file open fh using "Turnout.tex", write replace
	file write fh "\begin{tabular}{l c c c c c l}\hline\hline" _n
	file write fh " & \textbf{Obs.} & \textbf{Total} & \textbf{Treatment} & \textbf{Control} & \textbf{T - C} & \textbf{P-value} \\" _n
	file write fh "\hline" _n

	local name1="Extreme Poor" 
	local name2="Poor" 
	local name3="Non-Poor" 

foreach num of numlist 3 2 1 {
	qui sum  vote102 if  pobreza==`num'
			local 	mean0=string(r(mean),"%5.3f")
	qui sum  vote102 if pobreza==`num' & treat==1
			local 	mean1=string(r(mean),"%5.3f")	
	qui sum  vote102 if pobreza==`num' & treat==0
			local 	mean2=string(r(mean),"%5.3f")	
	qui ttest  vote102 if pobreza==`num', by(treat)
		local diff1=string(r(mu_2)-r(mu_1),"%5.3f")
		local pdiff1=string((r(p)),"%5.3f")
		local N1=string(r(N_1)+r(N_2),"%-9.0f")

file write fh "\multicolumn{1}{l}{`name`num''} & `N1' & `mean0' & `mean1' & `mean2' & `diff1' & (`pdiff1') \\" _n
}
file write fh "\hline" _n
	qui sum  vote102 
			local 	mean0=string(r(mean),"%5.3f")
	qui sum  vote102 if treat==1
			local 	mean1=string(r(mean),"%5.3f")	
	qui sum  vote102 if treat==0
			local 	mean2=string(r(mean),"%5.3f")	
	qui ttest vote102, by(treat)
		local diff1=string(r(mu_2)-r(mu_1),"%5.3f")
		local pdiff1=string((r(p)),"%5.3f")
		local N1=string(r(N_1)+r(N_2),"%-9.0f")

file write fh "\multicolumn{1}{l}{Total} & `N1' & `mean0' & `mean1' & `mean2' & `diff1' & (`pdiff1') \\" _n

file write fh "\hline" _n
file write fh "\hline" _n
file write fh "\end{tabular}" _n
file close fh

* Table 2: Balance Between Treatment and Control Groups

* Exclude observations that don't enter the analysis sample
xi: ivreg2		vote102 (deltam=$instruments) $controls i.ubigeo_ccpp if panel_complete==1 & pobreza>1, cl(hhid)    partial(i.ubigeo_ccpp) nocons
keep if e(sample)


label var multa_1 "Perceived Fine (Baseline)"
label var policyq1 "Policy Extreme 1 (Pub. goods)"
label var policyq5 "Policy Extreme 2 (Club goods)"
label var scoreinfo1 "Political information score"

* Table 2: Balance: Treatment and control in the analysis sample

cap file close fh
cap prog drop sum1/
program define sum1
	confirm numeric variable `1'
	confirm numeric variable `2'
	confirm e `4'
	qui sum  `1' 
		local 	mean=string(r(mean),"%5.2f")
		local 	sd=string(r(sd),"%5.2f")
	qui sum  `1' if `2'==0 
		local 	mean0=string(r(mean),"%5.2f")
		local 	sd0=string(r(sd),"%5.2f")
		local 	N_0=string(r(N),"%-9.0f")
	qui sum  `1' if `2'==1 
		local 	mean1=string(r(mean),"%5.2f")
		local 	sd1=string(r(sd),"%5.2f")
		local 	N_1=string(r(N),"%-9.0f")
	qui ttest `1', by(`2')
		local diff1=string(r(mu_2)-r(mu_1),"%5.2f")
		local pdiff1=string(r(p),"%5.2f")
		local N1=string(r(N_1)+r(N_2),"%-9.0f")
		local name : var label `1'

file write `4' "`name' & `N1' & `mean' & `sd' & `N_1' & `mean1' & `sd1' & `N_0' & `mean0' & `sd0' & `diff1' & (`pdiff1')\\" _n
end

file open fh using "balance_treat.tex", write replace
	file write fh "\begin{tabular}{l c c c c c c c c c c c}\hline\hline" _n
		file write fh "\textbf{Variable} & \multicolumn{3}{c}{\textbf{Full Analysis Sample}} & \multicolumn{3}{c}{\textbf{Treatment}} & \multicolumn{3}{c}{\textbf{Control}} & \textbf{T - C} & \textbf{P-value}\\" _n
		file write fh "\textbf{} & \textbf{Obs.} & \textbf{Mean} & \textbf{Std. Dev.} & \textbf{Obs.} & \textbf{Mean} & \textbf{Std. Dev.} & \textbf{Obs.} & \textbf{Mean} & \textbf{Std. Dev.} & \textbf{} & \textbf{}\\ \hline" _n
		
	foreach var of varlist multa_1 gender age educ gasto_pc center izq der policyq1 policyq234 policyq5 interesadopolitica2d1 interesadopolitica2d2 interesadopolitica2d3 resultsinterestedd1 resultsinterestedd2 resultsinterestedd3 campaigninterestedd1 campaigninterestedd2 campaigninterestedd3 canknow partknow knowtot scoreinfo1 {
		sum1 `var'          treat              "`1'"    fh
	}
	file write fh "\hline" _n
	file write fh "\hline" _n
	file write fh "\end{tabular}" _n
	file close fh
prog drop sum1

*joint test
foreach var of varlist multa_1 gender age educ gasto_pc center izq der policyq1 policyq234 policyq5 interesadopolitica2d1 interesadopolitica2d2 interesadopolitica2d3 resultsinterestedd1 resultsinterestedd2 resultsinterestedd3 campaigninterestedd1 campaigninterestedd2 campaigninterestedd3 canknow partknow knowtot scoreinfo1 { 
   reg `var' treat
   estimates store `var'
   }

   suest multa_1 gender age educ gasto_pc center izq der policyq1 policyq234 policyq5 interesadopolitica2d1 interesadopolitica2d2 interesadopolitica2d3 resultsinterestedd1 resultsinterestedd2 resultsinterestedd3 campaigninterestedd1 campaigninterestedd2 campaigninterestedd3 canknow partknow knowtot scoreinfo1 
test treat

*joint test w/o gender
foreach var of varlist multa_1 age educ gasto_pc center izq der policyq1 policyq234 policyq5 interesadopolitica2d1 interesadopolitica2d2 interesadopolitica2d3 resultsinterestedd1 resultsinterestedd2 resultsinterestedd3 campaigninterestedd1 campaigninterestedd2 campaigninterestedd3 canknow partknow knowtot scoreinfo1 { 
   reg `var' treat
   estimates store `var'
   }
   suest multa_1 age educ gasto_pc policyq1 policyq234 policyq5 interesadopolitica2d1 interesadopolitica2d2 interesadopolitica2d3 resultsinterestedd1 resultsinterestedd2 resultsinterestedd3 campaigninterestedd1 campaigninterestedd2 campaigninterestedd3 canknow partknow knowtot scoreinfo1 
test treat


***************
* Regressions * 
***************

use		"$data/Voting_Panel_Replication.dta", clear

label var pobre3	"Votes in Non-Poor district"

global controls="gender age educ lgastopc pobre2"
global instruments="nopobretreat pobretreat"

* I EXCLUDE FROM HERE ALL THE VOTERS IN EXTREMELY POOR DISTRICTS
xi: ivreg2		vote102 (deltam=$instruments) $controls i.ubigeo_ccpp if panel_complete==1, cl(hhid)    partial(i.ubigeo_ccpp) 
keep if e(sample)
drop	if pobreza==1


* Table 3: Main Results
reg	vote102 $instruments $controls i.ubigeo_ccpp, cl(hhid) 
	est2vec T3_Main, replace vars(deltam $instruments) e(r2) name(uno)
	sum	vote102 if e(sample)
reg	deltam $instruments $controls i.ubigeo_ccpp, cl(hhid) 
	est2vec T3_Main, addto(T3_Main) name(dos)
	sum deltam if e(sample)
xi: ivreg2		vote102 (deltam=$instruments) $controls  i.ubigeo_ccpp, cl(hhid) partial(i.ubigeo_ccpp) first
	est2vec T3_Main, addto(T3_Main) name(tres)
	sum vote102 if e(sample)
xi: ivreg2		vote063 (deltam=$instruments) $controls i.ubigeo_ccpp, cl(hhid) partial(i.ubigeo_ccpp) first
	est2vec T3_Main, addto(T3_Main) name(cuatro)
	sum vote063 if e(sample)
est2tex T3_Main, replace preserve mark(stars) levels(90 95 99)  flexible(3) digits(4)  fancy leadzero  label plain() thousep collab("(1)" "(2)" "(3)" "(4)")

* Table 4: Robustness: Determinants of Attrition and ID

* Panel A: Lee bounds for attrition: Trim the control group under the best and worst case scenarios.

preserve
		use		"$data/Voting_Panel_Replication.dta", clear
		cd "$out"
		
		xi: ivreg2		vote102 (deltam=$instruments) $controls  i.ubigeo_ccpp, cl(hhid)    partial(i.ubigeo_ccpp) 
			*keep if e(sample)
			keep 	if pobreza>1 
			keep 	if followup_only==0

		label var treat "Treatment"
		label var multa_1 "Perceived Fine (Baseline)"
		label var policyq1 "Policy Extreme 1 (Pub. goods)"
		label var policyq5 "Policy Extreme 2 (Club goods)"
		label var scoreinfo1 "Political information score"

		g attriter=(panel_complete==0)

		* Treatment status and attrition
		*reg	attriter treat $controls i.ubigeo_ccpp, cl(hhid) 
		*		est2vec TA_LeeBoundAttrition, replace vars(deltam $controls) e(widstat) name(Attrition)

		* Benchmark regression
		reg	vote102 treat, cl(hhid) 		
		
		leebounds vote102 treat, tight(pobre2 pobre3) cie vce(bootstrap)

		*reg deltam treat, cl(hhid)
		*leebounds deltam treat, tight(pobre2 pobre3) cie vce(bootstrap)
restore


* Panel B: Robustness - Regressions with different measures of voting
xi: ivreg2		vote102 (deltam=$instruments) $controls i.ubigeo_ccpp, cl(hhid) partial(i.ubigeo_ccpp) 
		sum	vote102 if e(sample)
		est2vec T4_Measurement, replace vars(deltam $controls) e(widstat r2) name(Benckmark)
xi: ivreg2		vote10SR (deltam=$instruments) $controls i.ubigeo_ccpp, cl(hhid) partial(i.ubigeo_ccpp) 
		sum	vote10SR if e(sample)
		est2vec T4_Measurement, addto(T4_Measurement) name(SelfReported)
xi: ivreg2		vote10Sticker (deltam=$instruments) $controls i.ubigeo_ccpp, cl(hhid) partial(i.ubigeo_ccpp) 
		sum	vote10Sticker if e(sample)
		est2vec T4_Measurement, addto(T4_Measurement) name(Sticker)
xi: ivreg2		vote10SR (deltam=$instruments) $controls i.ubigeo_ccpp if vote10Sticker~=. & vote10SR~=., cl(hhid)    partial(i.ubigeo_ccpp) 
		sum	vote10SR if e(sample)
		est2vec T4_Measurement, addto(T4_Measurement) name(Sample1)
xi: ivreg2		vote10Sticker (deltam=$instruments) $controls i.ubigeo_ccpp if vote10Sticker~=. & vote10SR~=., cl(hhid)    partial(i.ubigeo_ccpp) 
		sum	vote10Sticker if e(sample)
		est2vec T4_Measurement, addto(T4_Measurement) name(Sample2)
est2tex T4_Measurement, replace preserve mark(stars) levels(90 95 99) flexible(3) digits(4) fancy leadzero  label plain() thousep collab("(1)" "(2)" "(3)" "(4)" "(5)" "(6)")

* Panel C: Determinants of showing the ID and Lee bounds for not showing ID: Trim the control group under the best and worst case scenarios.
preserve
		use		"$data/Voting_Panel_Replication.dta", clear

		xi: ivreg2		vote102 (deltam=$instruments) $controls  i.ubigeo_ccpp, cl(hhid)    partial(i.ubigeo_ccpp) 
			keep if e(sample)
			keep 	if pobreza>1 
			keep 	if followup_only==0
			
		gen 	ID=(vote10Sticker~=.)
		gen 	noID=(ID==0 & ID~=.)

				reg	noID treat $controls i.ubigeo_ccpp, cl(hhid) 
						est2vec TA_LeeBound_ID, replace vars(treat deltam $controls) e(widstat) name(Attrition)
						sum noID if e(sample)

		bys treat: tab noID
			* noID in control=35.21
			* noID in treatment=34.30
					*--> difference in attrition: .91

		* Benchmark regression
		reg	vote10Sticker treat, cl(hhid) 		
		
		leebounds vote10Sticker treat, tight(pobre2 pobre3) cie vce(bootstrap)

		* to check how would the first stage change, assume that you don't observe deltam also for those without ID.
		*g deltam_lee=deltam if  vote10Sticker~=.
		
		*reg deltam_lee treat , cl(hhid)
		*leebounds deltam_lee treat , tight(pobre2 pobre3) cie vce(bootstrap)

restore

* Table 5: Robustness:
* Panel A: Including voters from extremely poor districts
preserve
use		"$data/Voting_Panel_Replication.dta", clear

		xi: ivreg2 vote102 (deltam=nopobretreat pobretreat  pobreexttreat) gender age educ lgastopc pobre3 pobre2 pobre1  i.ubigeo_ccpp, cl(hhid) partial(i.ubigeo_ccpp) 
		keep if e(sample)
		
		reg	vote102 nopobretreat pobretreat  pobreexttreat gender age educ lgastopc pobre3 pobre2 pobre1 i.ubigeo_ccpp, cl(hhid) 
					sum	vote102 if e(sample)
					est2vec T5_IV_EP, replace vars(deltam $instruments pobreexttreat) e(widstat r2) name(RF)
		reg	deltam nopobretreat pobretreat  pobreexttreat gender age educ lgastopc pobre3 pobre2 pobre1 i.ubigeo_ccpp, cl(hhid) 
					sum	deltam if e(sample)
					est2vec T5_IV_EP, addto(T5_IV_EP) name(FS)
		xi: ivreg2 vote102 (deltam=nopobretreat pobretreat  pobreexttreat) gender age educ lgastopc pobre3 pobre2 pobre1 i.ubigeo_ccpp, cl(hhid) partial(i.ubigeo_ccpp) 
					sum	vote102 if e(sample)
					est2vec T5_IV_EP, addto(T5_IV_EP) name(IV)
		est2tex T5_IV_EP, replace preserve mark(stars) levels(90 95 99) flexible(3) digits(4) fancy leadzero  label plain() thousep collab("(1)" "(2)" "(3)")
restore

* Panel B: Taking out potential defiers
reg	vote102 $instruments $controls i.ubigeo_ccpp if (multa_1>=36 & pobreza==2) | (multa_1>=70 & pobreza==3), cl(hhid) 
	est2vec T5_Monotonicity, replace vars(deltam $instruments) e(widstat r2) name(uno)
	sum	vote102 if e(sample)
reg	deltam $instruments $controls i.ubigeo_ccpp if (multa_1>=36 & pobreza==2) | (multa_1>=70 & pobreza==3), cl(hhid) 
	est2vec T5_Monotonicity, addto(T5_Monotonicity) name(dos)
	sum deltam if e(sample)
xi: ivreg2		vote102 (deltam=$instruments) $controls i.ubigeo_ccpp if (multa_1>=36 & pobreza==2) | (multa_1>=70 & pobreza==3), cl(hhid)    partial(i.ubigeo_ccpp) 
	est2vec T5_Monotonicity, addto(T5_Monotonicity) name(tres)
est2tex T5_Monotonicity, replace preserve mark(stars) levels(90 95 99) flexible(3) digits(4) fancy leadzero  label plain() thousep collab("(1)" "(2)" "(3)")

* Table 6: ROBUSTNESS check for violations to the exclusion restriction:
* Change in fine should only affect voting through change in fine, check if other consequences of voting didn't change as well.
ren	c10aa_2 consequence_fine
	label var consequence_fine "Consequence: Pay a fine" 
ren	c10ab_2 consequence_bank
	label var consequence_bank "Consequence: Can't use banks" 
ren	c10ac_2 consequence_municipality
	label var consequence_municipality "Consequence: Can't use municipality" 
ren	c10ad_2 consequence_notary
	label var consequence_notary "Consequence: Can't use notary" 
ren	c10ae_2 consequence_rrpp
	label var consequence_rrpp "Consequence: Can't use registry" 
ren	c10ag_2 consequence_other
	label var consequence_other "Consequence: Can't do other transactions" 

xi: reg	consequence_fine $instruments $controls i.ubigeo_ccpp if pobreza>1 & panel_complete==1, cl(hhid)
			est2vec T6_Robust_services1, replace vars($instruments) e(r2) name(uno)
			sum consequence_fine if e(sample)
foreach var of varlist consequence_bank consequence_municipality consequence_notary {
		xi: reg	`var' $instruments $controls i.ubigeo_ccpp if pobreza>1 & panel_complete==1, cl(hhid)
			est2vec T6_Robust_services1, addto(T6_Robust_services1) name(`var')
			sum `var' if e(sample)
}
	est2tex T6_Robust_services1, replace preserve mark(stars) levels(90 95 99) flexible(3) digits(4) fancy leadzero  label plain() thousep collab("(1)" "(2)" "(3)" "(4)")


xi: reg	consequence_rrpp $instruments $controls i.ubigeo_ccpp if pobreza>1 & panel_complete==1, cl(hhid)
			est2vec T6_Robust_services2, replace vars($instruments) e(r2) name(uno)
			sum consequence_rrpp if e(sample)
foreach var of varlist consequence_other campaign_interested_d1_2 results_interested_d1_2 {
		xi: reg	`var' $instruments $controls i.ubigeo_ccpp if pobreza>1 & panel_complete==1, cl(hhid)
			est2vec T6_Robust_services2, addto(T6_Robust_services2) name(`var')
			sum `var' if e(sample)
}
	est2tex T6_Robust_services2, replace preserve mark(stars) levels(90 95 99) flexible(3) digits(4) fancy leadzero  label plain() thousep collab("(1)" "(2)" "(3)" "(4)")

*************************
* Interactions
**************************

* Table 7A: Political preferences -  left right

foreach var1 of varlist izq center der policyq1 policyq234 policyq5 {
			g		NPT`var1'=nopobretreat*`var1'
			g		PT`var1'=pobretreat*`var1'
			*g		T`var1'=treat*`var1'
			g		NP`var1'=pobre3*`var1'
			g		P`var1'=pobre2*`var1'
			g		DM`var1'=deltam*`var1'
}

label var	DMizq "$\triangle$ Fine*Left"
label var	DMcenter "$\triangle$ Fine*Center"
label var	DMder "$\triangle$ Fine*Right"
label var	DMpolicyq1 "$\triangle$ Fine*Policy Extreme 1"
label var	DMpolicyq234 "$\triangle$ Fine*Policy Center"
label var	DMpolicyq5 "$\triangle$ Fine*Policy Extreme 2"
	
xi: ivreg2		vote102 (DMizq DMcenter DMder= NPTcenter PTcenter NPTizq PTizq NPTder PTder) NPder Pder NPcenter Pcenter Pizq gender age educ lgastopc izq der interesadopolitica2d2 interesadopolitica2d3 canknow $controls i.ubigeo_ccpp, cl(hhid)   partial(i.ubigeo_ccpp)  
				est2vec T7_polpref, replace vars(DMizq DMcenter DMder DMpolicyq1 DMpolicyq234 DMpolicyq5) e(r2) name(uno)
				gen uno=e(sample)
xi: ivreg2		vote102 (DMpolicyq1 DMpolicyq234 DMpolicyq5= NPTpolicyq1 PTpolicyq1 NPTpolicyq234 PTpolicyq234 NPTpolicyq5 PTpolicyq5) NPpolicyq234 Ppolicyq234 NPpolicyq1 Ppolicyq1 Ppolicyq5 interesadopolitica2d2 interesadopolitica2d3 canknow $controls  i.ubigeo_ccpp if uno==1, cl(hhid)   partial(i.ubigeo_ccpp)  
				est2vec T7_polpref, addto(T7_polpref) name(dos)
				drop uno	
est2tex T7_polpref, replace preserve mark(stars) levels(90 95 99) flexible(3) digits(4) fancy leadzero  label plain() thousep

drop	DM* NPT* PT* P* NP* 

* Table 7B: Interest in politics
foreach var1 of varlist interesadopolitica2d1 interesadopolitica2d2 interesadopolitica2d3 campaigninterestedd1 campaigninterestedd2 campaigninterestedd3 resultsinterestedd1 resultsinterestedd2 resultsinterestedd3 {
			g		NPT`var1'=nopobretreat*`var1'
			g		PT`var1'=pobretreat*`var1'
			g		NP`var1'=pobre3*`var1'
			g		P`var1'=pobre2*`var1'
			g		DM`var1'=deltam*`var1'
}

label var	DMinteresadopolitica2d1 "$\triangle$ Fine*Very interested in politics"
label var	DMinteresadopolitica2d2 "$\triangle$ Fine*Interested in politics"
label var	DMinteresadopolitica2d3 "$\triangle$ Fine*Not interested in politics"
label var	DMcampaigninterestedd1 "$\triangle$ Fine*Very interested in pol. campaign"
label var	DMcampaigninterestedd2 "$\triangle$ Fine*Interested in pol. campaign"
label var	DMcampaigninterestedd3 "$\triangle$ Fine*Not interested in pol. campaign"
label var	DMresultsinterestedd1 "$\triangle$ Fine*Very interested in results"
label var	DMresultsinterestedd2 "$\triangle$ Fine*Interested in results"
label var	DMresultsinterestedd3 "$\triangle$ Fine*Not interested in results"

xi: ivreg2		vote102 (DMinteresadopolitica2d*= PTinteresadopolitica2d* NPTinteresadopolitica2d*) NPinteresadopolitica2d1 Pinteresadopolitica2d1 NPinteresadopolitica2d2 Pinteresadopolitica2d2 Pinteresadopolitica2d3 izq der canknow $controls  i.ubigeo_ccpp, cl(hhid) partial(i.ubigeo_ccpp) 
				est2vec T7_interestpol, replace vars(DMinteresadopolitica2d1 DMinteresadopolitica2d2 DMinteresadopolitica2d3  DMresultsinterestedd1 DMresultsinterestedd2 DMresultsinterestedd3 DMcampaigninterestedd1 DMcampaigninterestedd2 DMcampaigninterestedd3) e(r2) name(uno)
xi: ivreg2		vote102 (DMresultsinterestedd*= PTresultsinterestedd* NPTresultsinterestedd*) NPresultsinterestedd1 Presultsinterestedd1 NPresultsinterestedd2 Presultsinterestedd2 Presultsinterestedd3 izq der canknow $controls  i.ubigeo_ccpp, cl(hhid)  partial(i.ubigeo_ccpp) 
				est2vec T7_interestpol, addto(T7_interestpol) name(tres)
xi: ivreg2		vote102 (DMcampaigninterestedd*=  PTcampaigninterestedd* NPTcampaigninterestedd*) NPcampaigninterestedd1 Pcampaigninterestedd1 NPcampaigninterestedd2 Pcampaigninterestedd2 Pcampaigninterestedd3 izq der $controls  i.ubigeo_ccpp, cl(hhid) partial(i.ubigeo_ccpp) 
				est2vec T7_interestpol, addto(T7_interestpol) name(dos)
est2tex T7_interestpol, replace preserve mark(stars) levels(90 95 99) flexible(3) digits(4) fancy leadzero  label plain() thousep
drop	DM* NPT* PT* P* NP* 
	
* Table 7C: Information		
foreach var1 of varlist scoreinfo1 canknow partknow knowtot {
			g		NPT`var1'=nopobretreat*`var1'
			g		PT`var1'=pobretreat*`var1'
			g		NP`var1'=pobre3*`var1'
			g		P`var1'=pobre2*`var1'
			g		DM`var1'=deltam*`var1'
}
	 
label var  DMscoreinfo1 "$\triangle$ Fine*Pol. Info. Score"
label var  DMcanknow "$\triangle$ Fine*Candidate recall"
label var  DMpartknow "$\triangle$ Fine*Party recall"
label var  DMknowtot "$\triangle$ Fine*Candidate and Party recall"

label var  scoreinfo1 "Political Information Score"
label var  canknow "Name recall-Candidates"
label var  partknow "Name recall-Parties" 
label var  knowtot "Name recall-Candidates and Parties"

xi: ivreg2		vote102 (DMcanknow deltam= PTcanknow NPTcanknow $instruments) NPcanknow Pcanknow izq der interesadopolitica2d2 interesadopolitica2d3 $controls i.ubigeo_ccpp, cl(hhid) partial(i.ubigeo_ccpp) 
				est2vec T7_info, replace vars(deltam DMcanknow DMpartknow DMknowtot DMscoreinfo1) e(r2) name(uno)
foreach var1 of varlist  partknow knowtot scoreinfo1 {
	xi: ivreg2	vote102 (DM`var1' deltam= PT`var1' NPT`var1' $instruments) NP`var1' P`var1' izq der interesadopolitica2d2 interesadopolitica2d3 $controls i.ubigeo_ccpp, cl(hhid) partial(i.ubigeo_ccpp)  
				est2vec T7_info, addto(T7_info) name(`var1')
}
est2tex T7_info, replace preserve mark(stars) levels(90 95 99) flexible(3) digits(4) fancy leadzero  label plain() thousep
		drop	DM* NPT* PT* P* NP* 
		
		
* Table 8: Effects by policy preferences
foreach var1 of varlist policy3* {
				g		NPT`var1'=nopobretreat*`var1'
				g		PT`var1'=pobretreat*`var1'
				g		NP`var1'=pobre3*`var1'
				g		P`var1'=pobre2*`var1'
				g		DM`var1'=deltam*`var1'
}

label var  DMpolicy3salud "$\triangle$ Fine*Health"
label var  DMpolicy3educ  "$\triangle$ Fine*Education" 
label var  DMpolicy3infra  "$\triangle$ Fine*Infrastructure"
label var  DMpolicy3orden  "$\triangle$ Fine*Order and Security"
label var  DMpolicy3econ   "$\triangle$ Fine*Promote micro-enterprises/training"
label var  DMpolicy3agro   "$\triangle$ Fine*Agriculture"
label var  DMpolicy3minority  "$\triangle$ Fine*Youth/Women"
label var  DMpolicy3enviro "$\triangle$ Fine*Cleaning/Environment"
label var  DMpolicy3inst   "$\triangle$ Fine*Institutions"
label var  DMpolicy3prog   "$\triangle$ Fine*Social/work programs"

xi: ivreg2		vote102 (DMpolicy3salud deltam= PTpolicy3salud NPTpolicy3salud nopobretreat pobretreat) $controls NPpolicy3salud Ppolicy3salud i.ubigeo_ccpp, cl(hhid) partial(i.ubigeo_ccpp)   
				est2vec T8_policypref, replace vars(deltam DMpolicy3salud DMpolicy3educ DMpolicy3infra DMpolicy3orden DMpolicy3econ DMpolicy3agro DMpolicy3minority DMpolicy3enviro DMpolicy3inst DMpolicy3prog) e(r2) name(policy3health)
foreach var of varlist policy3educ policy3infra policy3orden policy3econ policy3agro policy3minority policy3enviro policy3inst policy3prog { 
d `var'
	xi: ivreg2		vote102 (DM`var' deltam= PT`var' NPT`var' nopobretreat pobretreat) $controls NP`var' P`var' i.ubigeo_ccpp, cl(hhid) partial(i.ubigeo_ccpp)   
					est2vec T8_policypref, addto(T8_policypref) name(`var')
}
est2tex T8_policypref, replace preserve mark(stars) levels(90 95 99) flexible(3) digits(4) fancy leadzero  label plain() thousep
drop	DM* NPT* PT* P* NP* 

* Table 9A: Effects of Fines on Information Acquisition
gen		chscore=(score_info2-scoreinfo1)
gen		chcan=(canknow_2-canknow)
gen		chpart=(partknow_2 -partknow)
gen		chknow=(knowtot_2 -knowtot)
	
label var chscore "$\triangle$ pol. info. score"
label var chcan "$\triangle$ Candidate recall"
label var chpart "$\triangle$ Party recall"
label var chknow "$\triangle$ Candidate+Party Recall"
			
xi: ivreg2		chcan (deltam =$instruments) $controls i.ubigeo_ccpp, cl(hhid)    partial(i.ubigeo_ccpp) 
				sum chcan  if e(sample)
				est2vec T9_infoacq2, replace vars(deltam $controls) e(widstat r2) name(uno)
foreach var of varlist chpart chknow chscore {
	xi: ivreg2		`var' (deltam =$instruments) $controls i.ubigeo_ccpp, cl(hhid)    partial(i.ubigeo_ccpp) 
					est2vec T9_infoacq2, addto(T9_infoacq2) name(`var')
					sum `var' if e(sample)
}
est2tex T9_infoacq2, replace preserve mark(stars) levels(90 95 99) flexible(3) digits(4) fancy leadzero  label plain() thousep collab("(1)" "(2)" "(3)")

* Table 9B: Effects of Fines on Vote buying
ren compra_offer compraoffer
xi: ivreg2	compraoffer (deltam = $instruments) $controls i.ubigeo_ccpp, cl(hhid) partial(i.ubigeo_ccpp) 
			est2vec T9_compra, replace vars(deltam $controls) e(r2 widstat) name(compraoffer)
sum compraoffer  if e(sample)

foreach var1 of varlist compradirectmonto  compraindirectmonto compramontotot {
* trim outliers
replace `var1'=100 if `var1'>100
xi: ivreg2	`var1' (deltam = $instruments) $controls i.ubigeo_ccpp if compraoffer==1 , cl(hhid)  partial(i.ubigeo_ccpp) 
			sum `var1'  if e(sample)
			est2vec T9_compra, addto(T9_compra) name(`var1')
}
est2tex T9_compra, replace preserve mark(stars) levels(90 95 99) flexible(3) digits(4) fancy leadzero  label plain() thousep

*********************
*** APPPENDIX TABLES
*********************

* Table A.2: Balance Attrition

preserve
use		"$data/Voting_Panel_Replication.dta", clear

keep	if treat~=. & gender~=. & age~=. & educ~=. & gasto_pc~=. & pobre1~=. & pobre2~=. & pobre3~=. 
keep 	if pobreza>1
drop if followup_only==1

label var treat "Treatment"
label var multa_1 "Perceived Fine (Baseline)"
label var policyq1 "Policy Extreme 1 (Pub. goods)"
label var policyq5 "Policy Extreme 2 (Club goods)"
label var scoreinfo1 "Political information score"

cap file close fh
cap prog drop sum1\
program define sum1
	confirm numeric variable `1'
	confirm numeric variable `2'
	confirm e `4'
	qui sum  `1' if `2'==0 & followup_only==0
		local 	mean0=string(r(mean),"%5.2f")
		local 	sd0=string(r(sd),"%5.2f")
	qui sum  `1' if `2'==1 & followup_only==0
		local 	mean1=string(r(mean),"%5.2f")
		local 	sd1=string(r(sd),"%5.2f")
	qui ttest `1' if followup_only==0, by(`2')
			local diff1=string(r(mu_2)-r(mu_1),"%5.2f")
			local pdiff1=string(r(p),"%5.2f")
			local N1=string(r(N_1),"%-9.0f")
			local N2=string(r(N_2),"%-9.0f")
			local N3=string(r(N_1)+r(N_2),"%-9.0f")
			local name : var label `1'

file write `4' "`name' & `N1' & `mean0' & `sd0' & `N2' & `mean1' & `sd1' & `diff1' & (`pdiff1')\\" _n
end

file open fh using "balance_attrition.tex", write replace
	file write fh "\begin{tabular}{l c c c c c c c c}\hline\hline" _n
	file write fh " &  \multicolumn{3}{c}{\textbf{Attritters}} & \multicolumn{3}{c}{\textbf{Non-Attritters}} & \textbf{NA - A} & \textbf{P-value}\\" _n
	file write fh "\multicolumn{1}{c}{\textbf{Variable}} & \textbf{Obs.} & \textbf{Std. Dev.} & \textbf{Mean} & \textbf{Obs.} & \textbf{Std. Dev.} & \textbf{Mean} &  & \\ \hline" _n
		* treat policyq1 policyq234 policyq5
foreach var of varlist  multa_1 gender age educ lgastopc center izq der  interesadopolitica2d1 interesadopolitica2d2 interesadopolitica2d3 resultsinterestedd1 resultsinterestedd2 resultsinterestedd3 campaigninterestedd1 campaigninterestedd2 campaigninterestedd3 canknow partknow knowtot scoreinfo1 {
	sum1 `var'          panel_complete              "`1'"    fh
}
file write fh "\hline" _n
file write fh "\hline" _n
file write fh "\end{tabular}" _n
file close fh
prog drop sum1

* Joint test
foreach var of varlist multa_1 gender age educ lgastopc center izq der interesadopolitica2d1 interesadopolitica2d2 interesadopolitica2d3 resultsinterestedd1 resultsinterestedd2 resultsinterestedd3 campaigninterestedd1 campaigninterestedd2 campaigninterestedd3 canknow partknow knowtot scoreinfo1 { 
   reg `var' panel_complete
   estimates store `var'
   }
   suest multa_1 gender age educ lgastopc center izq der interesadopolitica2d1 interesadopolitica2d2 interesadopolitica2d3 resultsinterestedd1 resultsinterestedd2 resultsinterestedd3 campaigninterestedd1 campaigninterestedd2 campaigninterestedd3 canknow partknow knowtot scoreinfo1
test panel_complete

* Joint test without gender
foreach var of varlist multa_1  age educ lgastopc center izq der interesadopolitica2d1 interesadopolitica2d2 interesadopolitica2d3 resultsinterestedd1 resultsinterestedd2 resultsinterestedd3 campaigninterestedd1 campaigninterestedd2 campaigninterestedd3 canknow partknow knowtot scoreinfo1 { 
   reg `var' panel_complete
   estimates store `var'
   }
   suest multa_1 age educ lgastopc center izq der interesadopolitica2d1 interesadopolitica2d2 interesadopolitica2d3 resultsinterestedd1 resultsinterestedd2 resultsinterestedd3 campaigninterestedd1 campaigninterestedd2 campaigninterestedd3 canknow partknow knowtot scoreinfo1
test panel_complete

restore

* Table A.3: Learning about the fine before and after the election
* generate days between election and follow-up
gen 	days_elec_fu=dia_fu-3
		g days_elecfu_tp=days_elec_fu*pobretreat
		g days_elecfu_tnp=days_elec_fu*nopobretreat

		g	nopobre_days_electfu=pobre3*days_elec_fu
		g	pobre_days_electfu=pobre2*days_elec_fu

* generate days between treatment and election
gen		days_elec_baseline=days_between-days_elec_fu
		g days_elecbase_tp=days_elec_base*pobretreat
		g days_elecbase_tnp=days_elec_base*nopobretreat

		g	nopobre_days_electbase=pobre3*days_elec_base 
		g	pobre_days_electbase=pobre2*days_elec_base

reg deltam days_between pobre2 pobre3 if treat==1, cl(hhid) 
	est2vec T_days, replace vars(days_between days_elec_base days_elec_fu) e(r2) name(uno)
reg deltam days_between pobre2 pobre3 if treat==0, cl(hhid) 
	est2vec T_days, addto(T_days) name(dos)
reg deltam days_elec_base days_elec_fu pobre2 pobre3 if treat==1, cl(hhid) 
	est2vec T_days, addto(T_days) name(tres)
reg deltam days_elec_base days_elec_fu pobre2 pobre3 if treat==0, cl(hhid) 
	est2vec T_days, addto(T_days) name(cuatro)
est2tex T_days, replace preserve mark(stars) levels(90 95 99) flexible(3) digits(4) fancy leadzero  label plain() thousep collab("(1)" "(2)" "(3)" "(4)")

* Table A.5: Reduced Form, FS and 2SLS, controlling for pol preferences, value of voting and information
reg	vote102 $instruments $controls izq der interesadopolitica2d2 interesadopolitica2d3 canknow  i.ubigeo_ccpp, cl(hhid) 
	est2vec TA_5, replace vars(deltam $instruments $controls izq der interesadopolitica2d2 interesadopolitica2d3 canknow) e(widstat r2) name(RF)
	sum	vote102 if e(sample)
reg	deltam $instruments $controls izq der interesadopolitica2d2 interesadopolitica2d3 canknow  i.ubigeo_ccpp, cl(hhid) 
	sum	deltam if e(sample)
	est2vec TA_5, addto(TA_5) name(FS)
xi: ivreg2		vote102 (deltam=$instruments) $controls izq der interesadopolitica2d2 interesadopolitica2d3 canknow  i.ubigeo_ccpp, cl(hhid)    partial(i.ubigeo_ccpp) 
	sum	vote102 if e(sample)
	est2vec TA_5, addto(TA_5) name(IV)
est2tex TA_5, replace preserve mark(stars) levels(90 95 99) flexible(3) fancy leadzero label plain() thousep collab("(1)" "(2)" "(3)")

* T.A.6. Alternative measures of the main independent variable:
gen	lmulta_1=ln(multa_1+1)
gen	lmulta_2=ln(multa_2+1)

g	deltam_gasto=deltam/gasto
g	multa_1_gasto=multa_1/gasto
g	multa_2_gasto=multa_2/gasto

label var  deltam_gasto "$\triangle$ P. Fine/PC Exp."
label var  multa_1_gasto "P. Fine (Baseline)/PC Exp." 
label var  multa_2_gasto  "P. Fine (Follow-up)/PC Exp."
label var  lmulta_1  "Log(1+P. Fine (Baseline))"
label var  lmulta_2 "Log(1+P. Fine (Follow-up))"

xi: ivreg2		vote102 (deltam=$instruments) $controls  i.ubigeo_ccpp, cl(hhid)    partial(i.ubigeo_ccpp) 
		est2vec T_IV_alt, replace vars(deltam multa_2 lmulta_2 deltam_gasto multa_2_gasto multa_1 lmulta_1 multa_2_gasto  $controls) e(widstat r2) name(uno)

xi: ivreg2		vote102 (multa_2=$instruments) $controls multa_1 i.ubigeo_ccpp, cl(hhid) partial(i.ubigeo_ccpp) 
		est2vec T_IV_alt, addto(T_IV_alt) name(dos)
xi: ivreg2		vote102 (lmulta_2=$instruments) $controls lmulta_1 i.ubigeo_ccpp, cl(hhid) partial(i.ubigeo_ccpp) 
		est2vec T_IV_alt, addto(T_IV_alt) name(tres)

xi: ivreg2		vote102 (deltam_gasto=$instruments) $controls  i.ubigeo_ccpp if deltam_gasto>-1 & deltam_gasto<1, cl(hhid)    partial(i.ubigeo_ccpp) 
		est2vec T_IV_alt, addto(T_IV_alt) name(cuatro)
xi: ivreg2		vote102 (multa_2_gasto=$instruments) $controls multa_1_gasto i.ubigeo_ccpp if multa_2_gasto>0 & multa_2_gasto<2, cl(hhid) partial(i.ubigeo_ccpp) 
		est2vec T_IV_alt, addto(T_IV_alt) name(cinco)
est2tex T_IV_alt, replace preserve mark(stars) levels(90 95 99) flexible(3) digits(4) fancy leadzero  label plain() thousep collab("(1)" "(2)" "(3)" "(4)" "(5)")
